package com.feonix.common.basedao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;

import com.feonix.common.util.StringUtil;


public class Entity implements Cloneable {
	
	private String tableName = null;

	private boolean create = true;

	private EntityProperty idProperty = null;

	private List<EntityProperty> entityProperties = new ArrayList<EntityProperty>();

	private Map<String, String[]> indexes = new HashMap<String, String[]>();

	private Map<String, EntityProperty> entityPropertiesMap = new HashMap<String, EntityProperty>();

	private Map<String, EntityProperty> onlyQueryEntityPropertiesMap = new HashMap<String, EntityProperty>();

	private Map<String, EntityProperty> generalEntityPropertiesMap = new HashMap<String, EntityProperty>();

	public String getTableName() {
		return this.tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public boolean isCreate() {
		return this.create;
	}

	public void setCreate(boolean create) {
		this.create = create;
	}

	public EntityProperty getIdProperty() {
		return this.idProperty;
	}

	public void setIdProperty(EntityProperty idProperty) {
		this.idProperty = idProperty;
	}

	public List<EntityProperty> getEntityProperties() {
		return this.entityProperties;
	}

	public void addEntityProperty(EntityProperty entityProperty) {
		if (entityProperty == null) {
			return;
		}
		this.entityProperties.add(entityProperty);
		this.entityPropertiesMap.put(entityProperty.getColumnName(), entityProperty);
	}

	public Map<String, EntityProperty> getEntityPropertiesMap() {
		return this.entityPropertiesMap;
	}

	public void addIndex(String indexName, String[] fields) {
		if ((StringUtil.isNotEmpty(indexName)) && (ArrayUtils.isNotEmpty(fields)))
			this.indexes.put(indexName, fields);
	}

	public Map<String, String[]> getIndexes() {
		return this.indexes;
	}

	public void addOnlyQueryEntityProperty(EntityProperty entityProperty) {
		if (entityProperty == null) {
			return;
		}
		this.onlyQueryEntityPropertiesMap.put(entityProperty.getColumnName(), entityProperty);
	}

	public Map<String, EntityProperty> getOnlyQueryEntityPropertiesMap() {
		return this.onlyQueryEntityPropertiesMap;
	}

	public void addGeneralEntityProperty(EntityProperty entityProperty) {
		if (entityProperty == null) {
			return;
		}
		this.generalEntityPropertiesMap.put(entityProperty.getColumnName(), entityProperty);
	}

	public Map<String, EntityProperty> getGeneralEntityPropertiesMap() {
		return this.generalEntityPropertiesMap;
	}

	public EntityProperty getEntityProperty(String columnName) {
		EntityProperty entityProperty = (EntityProperty) this.entityPropertiesMap.get(columnName);
		if (entityProperty != null) {
			return entityProperty;
		}
		entityProperty = (EntityProperty) this.onlyQueryEntityPropertiesMap.get(columnName);
		if (entityProperty != null) {
			return entityProperty;
		}
		entityProperty = (EntityProperty) this.generalEntityPropertiesMap.get(columnName);
		if (entityProperty != null) {
			return entityProperty;
		}
		return entityProperty;
	}

	public Entity clone() {
		Entity entity = null;
		try {
			entity = (Entity) super.clone();
		} catch (CloneNotSupportedException cloneNotSupportedException) {
		}
		return entity;
	}

}
